/*
 * gateway.h
 *
 *  Created on: Mar 16, 2024
 *      Author: 25119
 */

#ifndef GATEWAY_H_
#define GATEWAY_H_

#include "A72_cmd.h"
#include "node.h"

#define GATEWAY_UART_HANDLE huart3  //网关使用的串口句柄
#define MAX_SCAN_NODES      10      //每次扫描节点显示的最大值
#define MAX_CUSTOM_GROUP    4       //最大自定义组
#define MAX_ONE_GROUP_NUM   10      //每组最大数量
#define MAX_SAVE_NODES      (4 * MAX_ONE_GROUP_NUM)

typedef enum
{
  node_elevator = 0,
  node_rotation,
  node_shift,
  node_power,
}Node_group; //这些属于非自定义组

typedef struct
{
  uint8_t cnt;
  uint8_t head;
  uint8_t tail;
  uint8_t mac[MAX_SCAN_NODES][6];
} Node_scan;

typedef struct Gateway_info Gateway_info;

struct Gateway_info
{
  uint8_t app_key[16];

  uint8_t mac[6];
  uint8_t addr[2];
  uint8_t program_info[4];
//  uint8_t network_statue[1];
  uint8_t network_key[16];

  Node_scan node_scan; //这里用了一个环形队列，但事实上没有合理的用到环形功能，也不需要有用环形

  Node_info *node_ram_base;
  uint32_t node_cnt;
  uint32_t node_ram_offset[4 + MAX_CUSTOM_GROUP][MAX_ONE_GROUP_NUM];  // 可以用Node_group索引，4表示最大分组数量，10表示每组最大节点数量
  Node_info node_all[MAX_SAVE_NODES];
};

extern int A72_reboot(Gateway_info *gateway);
extern int A72_restore(Gateway_info *gateway);
int A72_get_mac_addr(Gateway_info *gateway);
int A72_get_program_info(Gateway_info *gateway);
int A72_get_gateway_info(Gateway_info *gateway);
extern int A72_set_gateway_info(Gateway_info *gateway);
int A72_start_scan_node(Gateway_info *gateway);
extern int A72_stop_scan_node(Gateway_info *gateway);
int A72_try_record_node(Gateway_info *gateway);
int A72_scan_node(Gateway_info *gateway);
extern int scan_node_extract_one_mac(Node_scan *node, uint8_t index);
int A72_node_param_cfg(Gateway_info *gateway, uint8_t *p_mac, uint16_t addr);
int A72_node_bind(Gateway_info *gateway);
int A72_add_node(Gateway_info *gateway, uint8_t *p_mac);
int A72_delete_node(uint16_t addr);
int A72_add_group(uint16_t addr, uint8_t group);
int A72_delete_group(uint16_t addr, uint8_t group);
int A72_delete_all_group(uint16_t addr);
extern int A72G_uart_rx(UART_HandleTypeDef *huart, uint32_t time_out);
int A72G_rx_uart_it_cb(UART_HandleTypeDef *huart, bool reset);

#endif /* GATEWAY_H_ */
